<!--

    Copyright © 2016-2021 The Thingsboard Authors

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<div class="tb-details-buttons" fxLayout.xs="column">
  <button mat-raised-button color="primary" fxFlex.xs
          [disabled]="(isLoading$ | async) || !entity?.hasData"
          (click)="onEntityAction($event, 'uploadPackage')"
          [fxShow]="!isEdit">
    {{ 'ota-update.download' | translate }}
  </button>
  <button mat-raised-button color="primary" fxFlex.xs
          [disabled]="(isLoading$ | async)"
          (click)="onEntityAction($event, 'delete')"
          [fxShow]="!hideDelete() && !isEdit">
    {{ 'ota-update.delete' | translate }}
  </button>
  <div fxLayout="row" fxLayout.xs="column">
    <button mat-raised-button
            ngxClipboard
            (cbOnSuccess)="onPackageIdCopied()"
            [cbContent]="entity?.id?.id"
            [fxShow]="!isEdit">
      <mat-icon svgIcon="mdi:clipboard-arrow-left"></mat-icon>
      <span translate>ota-update.copyId</span>
    </button>
    <button mat-raised-button
            ngxClipboard
            (cbOnSuccess)="onPackageChecksumCopied()"
            [cbContent]="entity?.checksum"
            [fxShow]="!isEdit">
      <mat-icon svgIcon="mdi:clipboard-arrow-left"></mat-icon>
      <span translate>ota-update.copy-checksum</span>
    </button>
  </div>
</div>
<div class="mat-padding" fxLayout="column" fxLayoutGap="8px">
  <form [formGroup]="entityForm">
    <fieldset [disabled]="(isLoading$ | async) || !isEdit" fxLayout="column" fxLayoutGap="8px">
      <div fxLayout="row" fxLayoutGap.gt-xs="8px" fxLayout.xs="column">
        <mat-form-field class="mat-block" fxFlex="45">
          <mat-label translate>ota-update.title</mat-label>
          <input matInput formControlName="title" type="text" required [readonly]="!isAdd">
          <mat-error *ngIf="entityForm.get('title').hasError('required')">
            {{ 'ota-update.title-required' | translate }}
          </mat-error>
        </mat-form-field>
        <mat-form-field class="mat-block" fxFlex>
          <mat-label translate>ota-update.version</mat-label>
          <input matInput formControlName="version" type="text" required [readonly]="!isAdd">
          <mat-error *ngIf="entityForm.get('version').hasError('required')">
            {{ 'ota-update.version-required' | translate }}
          </mat-error>
        </mat-form-field>
      </div>
      <tb-device-profile-autocomplete
        formControlName="deviceProfileId"
        required
        [hint]="'ota-update.chose-compatible-device-profile'"
        [editProfileEnabled]="false"
        [addNewProfile]="false"
        [selectDefaultProfile]="true">
      </tb-device-profile-autocomplete>
      <mat-form-field class="mat-block">
        <mat-label translate>ota-update.package-type</mat-label>
        <mat-select formControlName="type" required>
          <mat-option *ngFor="let packageType of packageTypes" [value]="packageType">
            {{ otaUpdateTypeTranslationMap.get(packageType) | translate }}
          </mat-option>
        </mat-select>
      </mat-form-field>
      <div class="mat-caption" translate *ngIf="isAdd">ota-update.warning-after-save-no-edit</div>
      <div fxLayout="row" fxLayoutGap.gt-xs="8px" fxLayoutGap.sm="8px" fxLayout.xs="column" fxLayout.md="column">
        <mat-form-field class="mat-block" fxFlex="33">
          <mat-label translate>ota-update.checksum-algorithm</mat-label>
          <mat-select formControlName="checksumAlgorithm">
            <mat-option *ngFor="let checksumAlgorithm of checksumAlgorithms" [value]="checksumAlgorithm">
              {{ checksumAlgorithmTranslationMap.get(checksumAlgorithm) }}
            </mat-option>
          </mat-select>
        </mat-form-field>
        <mat-form-field class="mat-block" fxFlex>
          <mat-label translate>ota-update.checksum</mat-label>
          <input matInput formControlName="checksum" type="text" [readonly]="!isAdd">
        </mat-form-field>
      </div>
      <section *ngIf="isAdd">
        <tb-file-input
          formControlName="file"
          workFromFileObj="true"
          required
          dropLabel="{{'ota-update.drop-file' | translate}}">
        </tb-file-input>
      </section>
      <section *ngIf="!isAdd">
        <div fxLayout="row" fxLayoutGap.gt-md="8px" fxLayoutGap.sm="8px" fxLayout.xs="column" fxLayout.md="column">
          <mat-form-field class="mat-block" fxFlex="33">
            <mat-label translate>ota-update.file-name</mat-label>
            <input matInput formControlName="fileName" type="text" readonly>
          </mat-form-field>
          <mat-form-field class="mat-block" fxFlex>
            <mat-label translate>ota-update.file-size-bytes</mat-label>
            <input matInput formControlName="dataSize" type="text" readonly>
          </mat-form-field>
          <mat-form-field class="mat-block" fxFlex>
            <mat-label translate>ota-update.content-type</mat-label>
            <input matInput formControlName="contentType" type="text" readonly>
          </mat-form-field>
        </div>
      </section>
      <div formGroupName="additionalInfo">
        <mat-form-field class="mat-block">
          <mat-label translate>ota-update.description</mat-label>
          <textarea matInput formControlName="description" rows="2"></textarea>
        </mat-form-field>
      </div>
    </fieldset>
  </form>
</div>
